<?php
class update_action
{
    public $layout = NULL;
    public $user_id = NULL;
    public function __construct()
    {
        $this->view = new system_view();
        $this->model = new model__update_action_model();
        $this->rt = new handle_route();
    }
    public function index_controller()
    {
        $strHtml = $this->view->view_display('index');
        $msg = '';
        if(isset($_POST['update']))
        {
            $app_name = $_POST['app'];
            $this->update_action($app_name);
            $msg = 'Action Com đã cập nhật thành công từ: ' . $app_name;
        }
        
        $strHtml = $this->view->view_assign($strHtml,array('msg'=>$msg));
        return $strHtml;
    }
    
    public function update_action($app_name)
    {
        $app = ($app_name == 'admin')?'1':'2';
        $dir_com = realpath('../app/'.$app_name.'/controller');
        $file_dir = scandir($dir_com);
        foreach($file_dir as $key=>$file)
        {
            if($file == '.' || $file == '..' || strstr($file, 'php') == ''){
                unset($file_dir[$key]);
            }
        }
        
        if(!empty($file_dir))
        {
            $action_list = $this->model->md_ua_get_list_action();
            $action_curent = array();
            foreach($action_list as $action)
            {
                $action_table[$action['ActName']] = $action['ID'];
            }
            
            foreach($file_dir as $file)
            {
                $com_path = $dir_com . '/' . $file;
                $com_name = str_replace('.php', '', $file);
                require($com_path);
                $com_class_curent = get_class_methods('controller_'.$com_name);
                $get_com_id = $this->model->md_ua_get_com_id($com_name);
                $com_id = $get_com_id['ID'];
                
                foreach($com_class_curent as $key=>$hs)
                {
                    if($hs == '__construct' || $hs == '__call')
                    {
                        unset($com_class_curent[$key]);
                    }
                }
                foreach($com_class_curent as $key=>$hs)
                {
                    $action_name = str_replace('_action', '',$hs);
                    if(array_key_exists($action_name,$action_table)){
                        $action_id = $action_table[$action_name];
                        if($com_id != null)
                        {
                            
                            $check_action  = $this->model->md_ua_check_action($com_id, $action_id,$app);
                            if(empty($check_action)){
                                $data = array(
                                    'ActID'=>$action_id,
                                    'ComID'=>$com_id,
                                    'AppID'=>$app
                                );
                                $result = $this->model->md_ua_insert_action_com($data);
                            }
                        }   
                    }     
                }                
            }
        }
    }
}